package com.wl.shop.utils;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wl.shop.dao.DictDao;
import com.wl.shop.entity.Dict;

public class DictUtils {
	
	private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class);
	
	public static final String CACHE_DICT_MAP = "dictMap";
	
	public static List<Dict> getDictList(String type){
		@SuppressWarnings("unchecked")
		Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>)CacheUtils.get(CACHE_DICT_MAP);
		if (dictMap==null){
			dictMap = Maps.newHashMap();
			for (Dict dic : dictDao.selectList(new EntityWrapper<Dict>())) {
				List<Dict> dictlist = dictMap.get(dic.getType());
				if(dictlist != null){
					dictlist.add(dic);
				}else{
					dictMap.put(dic.getType(), Lists.newArrayList(dic));
				}
			}
			CacheUtils.put(CACHE_DICT_MAP, dictMap);
		}
		List<Dict> dictList = dictMap.get(type);
		if (dictList == null){
			dictList = Lists.newArrayList();
		}
		return dictList;
	}
	
	/**
	 * 返回字典列表（JSON）
	 * @param type
	 * @return
	 */
	public static String getDictListJson(String type){
		return JSONObject.toJSONString(getDictList(type));
	}
}
